using UnityEngine;
using System.Collections;

[RequireComponent(typeof(LineRenderer))]

public class FireTrap : MonoBehaviour
{
	public float height = 3.2f;
	public float speed = 2.0f;
	public float timingOffset = 0.0f;
	public float flameWidth = 12.0f;
	public int damage = 1;
	public GameObject hitEffect;
	
	
	private LineRenderer lineRenderer;
	private Vector3 originalPosition;
	private RaycastHit hit;
	private float lastHitTime = 0.0f;
	
	public void Start()
	{
		originalPosition = transform.position;
		lineRenderer = this.GetComponent<LineRenderer>();
		lineRenderer.SetPosition(1, Vector3.forward * flameWidth);
	}
	
	public void Update()
	{
		float offset = (1 + Mathf.Sin(Time.time * speed + timingOffset)) * height /2;
		transform.position = originalPosition + new Vector3(0, offset, 0);
		
		if(Time.time > lastHitTime + 0.25 && Physics.Raycast(transform.position, transform.forward,out hit, flameWidth))
		{
			if(hit.collider.tag == "Player" || hit.collider.tag == "Enemy")
			{
				Instantiate(hitEffect, hit.point, Quaternion.identity);
				hit.collider.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
				lastHitTime = Time.time;
			}
		}
	}
}